package hutech.ksv.view;

import hutech.ksv.common.KenhSinhVienContants;
import hutech.ksv.entity.QuanAnEntity;
import hutech.ksv.model.QuanAnDataModel;
import hutech.ksv.service.QuanAnService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class QuanAnManageBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	QuanAnService quananService;
	
	@Autowired
	UserLoginManagedBean userLoginManagedBean;

	private int maQA;
	private String diaChiQA;
	private String moTaQA;
	private boolean trangThaiDuyetQA;
	private String tenTKCQA;
	private String tenTKQTV;
	private String diaChiTK;
	private String hinhAnhQA;
	private QuanAnDataModel quanAnListDataModel;
	private String fileName;
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public QuanAnDataModel getQuanAnListDataModel() {
		return quanAnListDataModel;
	}

	public void setQuanAnListDataModel(QuanAnDataModel quanAnListDataModel) {
		this.quanAnListDataModel = quanAnListDataModel;
	}

	public String getDiaChiTK() {
		return diaChiTK;
	}

	public void setDiaChiTK(String diaChiTK) {
		this.diaChiTK = diaChiTK;
	}

	public int getMaQA() {
		return maQA;
	}

	public String getHinhAnhQA() {
		return hinhAnhQA;
	}

	public void setHinhAnhQA(String hinhAnhQA) {
		this.hinhAnhQA = hinhAnhQA;
	}

	public void setMaQA(int maQA) {
		this.maQA = maQA;
	}

	public String getDiaChiQA() {
		return diaChiQA;
	}

	public void setDiaChiQA(String diaChiQA) {
		this.diaChiQA = diaChiQA;
	}

	public String getMoTaQA() {
		return moTaQA;
	}

	public void setMoTaQA(String moTaQA) {
		this.moTaQA = moTaQA;
	}

	public boolean isTrangThaiDuyetQA() {
		return trangThaiDuyetQA;
	}

	public void setTrangThaiDuyetQA(boolean trangThaiDuyetQA) {
		this.trangThaiDuyetQA = trangThaiDuyetQA;
	}

	public String getTenTKCQA() {
		return tenTKCQA;
	}

	public void setTenTKCQA(String tenTKCQA) {
		this.tenTKCQA = tenTKCQA;
	}

	public String getTenTKQTV() {
		return tenTKQTV;
	}

	public void setTenTKQTV(String tenTKQTV) {
		this.tenTKQTV = tenTKQTV;
	}
	
	private UploadedFile file;
	 
    public UploadedFile getFile() {
        return file;
    }
 
    public void setFile(UploadedFile file) {
        this.file = file;
    }

	@PostConstruct
	public void init() {
		List<QuanAnEntity> danhSachQuanAn = quananService.loadThongTinQA();
		quanAnListDataModel = new QuanAnDataModel(danhSachQuanAn);
		userLoginManagedBean.setLoaiNhanXet(KenhSinhVienContants.PHIEU_NHAN_XET_QUAN_AN);
	}

	// tương ứng hàm PageLoad trong .NET
	public void onLoad() {
		List<QuanAnEntity> danhSachQuanAn = quananService.loadThongTinQA();
		quanAnListDataModel = new QuanAnDataModel(danhSachQuanAn);
		userLoginManagedBean.setLoaiNhanXet(KenhSinhVienContants.PHIEU_NHAN_XET_QUAN_AN);
	}

	public void reLoad() {
		maQA = 0;
		diaChiQA = "";
		moTaQA = "";
		trangThaiDuyetQA = false;
		tenTKCQA = "";
		tenTKQTV = "";
	}

	public boolean checkAllInput() {
		boolean result;
		if (diaChiQA.equals("") || diaChiQA == null) {
			result = false;
		} else if (moTaQA.equals("") || moTaQA == null) {
			result = false;
		} else if (tenTKCQA.equals("") || tenTKCQA == null) {
			result = false;
		} else if (tenTKQTV.equals("") || tenTKQTV == null) {
			result = false;
		} else {
			result = true;
		}
		return result;
	}
	
	public void copyFile(String fileName, InputStream in) {
		try {
			// write the inputStream to a FileOutputStream
			String destination = "C:\\Studying\\HUTECH\\final-project\\workspace\\kenhsinhvien\\src\\main\\webapp\\images";
			OutputStream out = new FileOutputStream(new File(destination
					+ fileName));

			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = in.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}

			in.close();
			out.flush();
			out.close();
			System.out.println("New file created!");
		} catch (IOException e) {
			System.out.println(e.getMessage());

		}

	}
	


	public void upload(FileUploadEvent event) {
		FacesMessage msg = new FacesMessage("Success! ", event.getFile()
				.getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
		try {
			copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
			fileName = event.getFile().getFileName();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public String insert() {
		hinhAnhQA = "images/" + fileName;
		try {
			if (checkAllInput() == false) {
				diaChiQA = "";
				moTaQA = "";
				trangThaiDuyetQA = false;
				tenTKCQA = "";
				tenTKQTV = "";
			} else {
				QuanAnEntity quanan = new QuanAnEntity();
				quanan.setDiaChiQA(diaChiQA);
				quanan.setMoTaQA(moTaQA);
				quanan.isTrangThaiDuyetQA();
				quanan.setTenTKCQA(tenTKCQA);
				quanan.setTaiKhoanQTV(tenTKQTV);
				quanan.setHinhAnhQA(hinhAnhQA);
				quananService.insertQuanAn(quanan);
				return "xemThongTinQA";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "testNavigate";
	}

	public String update() {
		hinhAnhQA = "images/" + fileName;
		try {
			if (checkAllInput() == false) {
				maQA = 0;
				diaChiQA = "";
				moTaQA = "";
				trangThaiDuyetQA = false;
				tenTKCQA = "";
				tenTKQTV = "";
			} else {
				QuanAnEntity quanan = new QuanAnEntity();
				quanan.setMaQA(maQA);
				quanan.setDiaChiQA(diaChiQA);
				quanan.setMoTaQA(moTaQA);
				quanan.isTrangThaiDuyetQA();
				quanan.setTenTKCQA(tenTKCQA);
				quanan.setTaiKhoanQTV(tenTKQTV);
				quanan.setHinhAnhQA(hinhAnhQA);
				quananService.updateQuanAn(quanan);
				return "xemThongTinQA";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "testNavigate";
	}

	public String timKiemQATheoDC() {
		if (diaChiTK != "") {
			loadDSQATheoDC();
		}
		return "";
	}

	// Load thong tin quan an
	public List<QuanAnEntity> loadThongTinQA() {
		return quananService.loadThongTinQA();
	}

	// Load quan an theo dia chi
	public QuanAnDataModel loadDSQATheoDC() {
		QuanAnEntity quanan = new QuanAnEntity();
		quanan.setDiaChiQA(diaChiTK);
		
		List<QuanAnEntity> danhSachQuanAn = quananService.loadDSQATheoDC(quanan);
		quanAnListDataModel = new QuanAnDataModel(danhSachQuanAn);
		
		return quanAnListDataModel;
	}

	public String edit() {
		QuanAnEntity quanAnEntity = new QuanAnEntity();
		quanAnEntity = quanAnListDataModel.getRowData();
		maQA = quanAnEntity.getMaQA();
		moTaQA = quanAnEntity.getMoTaQA();
		diaChiQA = quanAnEntity.getDiaChiQA();
		trangThaiDuyetQA = quanAnEntity.isTrangThaiDuyetQA();
		tenTKCQA = quanAnEntity.getTenTKCQA();
		tenTKQTV = quanAnEntity.getTaiKhoanQTV();
		hinhAnhQA = quanAnEntity.getHinhAnhQA();
		return "updateQuanAn";
		// return "testNavigate";
	}
	
	public String chitiet() {
		QuanAnEntity quanAnEntity = new QuanAnEntity();
		quanAnEntity = quanAnListDataModel.getRowData();
		maQA = quanAnEntity.getMaQA();
		moTaQA = quanAnEntity.getMoTaQA();
		diaChiQA = quanAnEntity.getDiaChiQA();
		trangThaiDuyetQA = quanAnEntity.isTrangThaiDuyetQA();
		tenTKCQA = quanAnEntity.getTenTKCQA();
		tenTKQTV = quanAnEntity.getTaiKhoanQTV();
		hinhAnhQA = quanAnEntity.getHinhAnhQA();
		userLoginManagedBean.setLoaiNhanXet(KenhSinhVienContants.PHIEU_NHAN_XET_QUAN_AN);
		return "chiTietQuanAn";
	}
}
